package com.newshub.app.ui.category

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.newshub.app.data.model.News
import com.newshub.app.databinding.FragmentCategoryNewsBinding
import com.newshub.app.ui.news.NewsDetailActivity
import com.newshub.app.ui.adapter.NewsAdapter
import com.newshub.app.ui.news.NewsViewModel

class CategoryNewsFragment : Fragment() {
    
    private var _binding: FragmentCategoryNewsBinding? = null
    private val binding get() = _binding!!
    
    private lateinit var viewModel: NewsViewModel
    private lateinit var newsAdapter: NewsAdapter
    private lateinit var category: String
    
    companion object {
        private const val ARG_CATEGORY = "category"
        
        fun newInstance(category: String): CategoryNewsFragment {
            val fragment = CategoryNewsFragment()
            val args = Bundle()
            args.putString(ARG_CATEGORY, category)
            fragment.arguments = args
            return fragment
        }
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        category = arguments?.getString(ARG_CATEGORY) ?: "推荐"
    }
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentCategoryNewsBinding.inflate(inflater, container, false)
        return binding.root
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        initViewModel()
        initRecyclerView()
        loadNews()
    }
    
    private fun initViewModel() {
        viewModel = ViewModelProvider(this)[NewsViewModel::class.java]
        
        // 观察新闻列表
        viewModel.newsList.observe(viewLifecycleOwner) { newsList ->
            newsAdapter.submitList(newsList)
        }
        
        // 观察加载状态
        viewModel.isLoading.observe(viewLifecycleOwner) { isLoading ->
            binding.progressBar.visibility = if (isLoading) View.VISIBLE else View.GONE
        }
        
        // 观察错误信息
        viewModel.errorMessage.observe(viewLifecycleOwner) { errorMessage ->
            if (errorMessage != null) {
                Toast.makeText(requireContext(), errorMessage, Toast.LENGTH_SHORT).show()
            }
        }
    }
    
    private fun initRecyclerView() {
        newsAdapter = NewsAdapter(
            onItemClick = { news ->
                openNewsDetail(news.docid)
            },
            onFavoriteClick = { news ->
                toggleFavorite(news)
            }
        )
        
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = newsAdapter
        }
    }
    
    private fun loadNews() {
        viewModel.loadCategoryNews(category)
    }
    
    private fun openNewsDetail(newsId: String) {
        val intent = Intent(requireContext(), NewsDetailActivity::class.java)
        intent.putExtra("news_id", newsId)
        startActivity(intent)
    }
    
    private fun toggleFavorite(news: News) {
        viewModel.toggleFavorite(news)
    }
    
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}